﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using NetTao.Config;
using NetTao.Dao;
using NetTao.Model;

namespace NetTao.Spider.Win
{
    public partial class FrmSpiderManage : Form
    {

        private  IList<ConfigInfo> _configs = new List<ConfigInfo>();

        private DaoArticle _daoArticle;

        private DaoChapter _daoChapter;

        private DaoAttachs _daoAttachs;

        private DaoSysWeb _daoSysWeb;

        public FrmSpiderManage()
        {
            InitializeComponent();

            #region 任务设置
            btnSelectFile.Click += new EventHandler(BtnSelectFileClick);
            #endregion

            btnQuery.Click += new EventHandler(BtnQueryClick);
            btnSelectAll.Click += new EventHandler(BtnSelectAllClick);
            //tvlist选择一个节点后,tpArticleInfo显示小说名称
        }

        void BtnSelectAllClick(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (TreeNode node in tvList.Nodes)
            {
                if (!node.Checked)
                    checkAll = false;
            }
            foreach (TreeNode node in tvList.Nodes)
            {
                node.Checked = !checkAll;
            }
        }

        private bool ValidateConfig()
        {
            if(string.IsNullOrEmpty(txtConfigPath.Text))
            {
                MessageBox.Show("先选择配置文件路径");
                this.tabControl1.SelectTab(0);
                txtConfigPath.Focus();
                return false;
            }
            return true;
        }

        void BtnQueryClick(object sender, EventArgs e)
        {
            if (!ValidateConfig()) return;
            
            string condition = "";
            if (!string.IsNullOrEmpty(txtArticleName.Text))
                condition += string.Format(" and articlename like '%{0}%'", txtArticleName.Text);

            if (!string.IsNullOrEmpty(txtChapterName.Text))
                condition += string.Format(" and chaptername like '%{0}%'", txtChapterName.Text);

            if (!string.IsNullOrEmpty(txtAuthor.Text))
                condition += string.Format(" and author like '%{0}%'", txtAuthor.Text);

            //if(!string.IsNullOrEmpty(cmbSearchWebs.Text))

            IList<ArticleInfo> list = _daoArticle.GetListByUpdateTime(condition);
            foreach (ArticleInfo articleInfo in list)
            {
                var node = new TreeNode {Checked = false, Text = articleInfo.articlename, Tag = articleInfo};
                tvList.Nodes.Add(node);
            }
        }

        void BtnSelectFileClick(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (!string.IsNullOrEmpty(openFileDialog1.FileName)
                && openFileDialog1.FileName != "openFileDialog1")
            {
                this.txtConfigPath.Text = openFileDialog1.FileName;
                LoadConfig(txtConfigPath.Text);
            }
        }

        private void LoadConfig(string path)
        {
            using (FileStream stream = File.Open(path, FileMode.Open))
            {
                var xmlserializer = new XmlSerializer(typeof (List<ConfigInfo>));
                _configs = xmlserializer.Deserialize(stream) as List<ConfigInfo>;
                stream.Close();
                stream.Dispose();
            }
            if (_configs != null && _configs.Count > 0)
            {
                tsslTask.Text = _configs.First().flag;
                tsslConnString.Text = _configs.First().ConnString;
                _daoArticle = new DaoArticle(_configs.First().ConnString);
                _daoChapter = new DaoChapter(_configs.First().ConnString);
                _daoAttachs = new DaoAttachs(_configs.First().ConnString);
                _daoSysWeb = new DaoSysWeb(_configs.First().ConnString);
            }
        }

        private void LoadWebs()
        {

        }
    }
}
